home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 008 / src / hack.save.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  5KB  |  223 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
  2.  
  3. #include "hack.h"
  4. extern char genocided[60];    /* defined in Decl.c */
  5. extern char fut_geno[60];    /* idem */
  6.  
  7. #ifndef AMIGA
  8. #include <signal.h>
  9. #endif
  10.  
  11. extern char SAVEF[], nul[];
  12. extern char pl_character[PL_CSIZ];
  13. extern long lseek();
  14. extern struct obj *restobjchn();
  15. extern struct monst *restmonchn();
  16.  
  17. extern char *index();        /* M.E.T.  11/20/85 */
  18.  
  19. dosave(){
  20.     if(dosave0(0)) {
  21.         settty("Be seeing you ...\n");
  22.         hackexit(0);
  23.     }
  24. #ifdef lint
  25.     return(0);
  26. #endif lint
  27. }
  28.  
  29. #ifndef NOSAVEONHANGUP
  30. hangup(){
  31.     (void) dosave0(1);
  32.     hackexit(1);
  33. }
  34. #endif NOSAVEONHANGUP
  35.  
  36. /* returns 1 if save successful */
  37. dosave0(hu) int hu; {
  38.     register int fd, ofd;
  39.     int tmp;      /* not register ! */
  40. #ifndef AMIGA
  41.     (void) signal(SIGHUP, SIG_IGN);
  42.     (void) signal(SIGINT, SIG_IGN);
  43. #endif
  44.     if((fd = creat(SAVEF, FMASK)) < 0)
  45.         {
  46.         if(!hu) pline("Cannot open save file. (Continue or Quit)");
  47.             return(0);
  48.         }
  49.     makeicon(SAVEF, pl_character[0]);
  50.     savelev(fd);
  51.     saveobjchn(fd, invent);
  52.     saveobjchn(fd, fcobj);
  53.     savemonchn(fd, fallen_down);
  54.     bwrite(fd, (char *) &flags, sizeof(struct flag));
  55.     bwrite(fd, (char *) &dlevel, sizeof dlevel);
  56.     bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
  57.     bwrite(fd, (char *) &moves, sizeof moves);
  58.     bwrite(fd, (char *) &u, sizeof(struct you));
  59.     bwrite(fd, (char *) pl_character, sizeof pl_character);
  60.     bwrite(fd, (char *) genocided, sizeof genocided);
  61.     bwrite(fd, (char *) fut_geno, sizeof fut_geno);
  62.     savenames(fd);
  63.     for(tmp = 1; tmp <= maxdlevel; tmp++)
  64.         {
  65.         glo(tmp);
  66.         if((ofd = open(lock, 0)) < 0)
  67.             continue;
  68.         (void) getlev(ofd);
  69.         (void) close(ofd);
  70.         bwrite(fd, (char *) &tmp, sizeof tmp);   /* level number */
  71.         savelev(fd);            /* actual level */
  72.         (void) unlink(lock);
  73.         }
  74.     (void) close(fd);
  75.     *index(lock, '.') = 0;
  76.     (void) unlink(lock);
  77.     return(1);
  78. }
  79.  
  80. dorecover(fd)
  81. register int fd;
  82. {
  83.     register int nfd;
  84.     int tmp;      /* not a register ! */
  85.     struct obj *otmp;
  86.      (void) getlev(fd);
  87.     invent = restobjchn(fd);
  88.     for(otmp = invent; otmp; otmp = otmp->nobj)
  89.         if(otmp->owornmask)
  90.             setworn(otmp, otmp->owornmask);
  91.     fcobj = restobjchn(fd);
  92.     fallen_down = restmonchn(fd);
  93.     mread(fd, (char *) &flags, sizeof(struct flag));
  94.     mread(fd, (char *) &dlevel, sizeof dlevel);
  95.     mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
  96.     mread(fd, (char *) &moves, sizeof moves);
  97.     mread(fd, (char *) &u, sizeof(struct you));
  98.     mread(fd, (char *) pl_character, sizeof pl_character);
  99.     mread(fd, (char *) genocided, sizeof genocided);
  100.     mread(fd, (char *) fut_geno, sizeof fut_geno);
  101.     restnames(fd);
  102.     while(1)
  103.         {
  104.         if(read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
  105.             break;
  106.         if(getlev(fd))
  107.             break;      /* this is actually an error */
  108.         glo(tmp);
  109.         if((nfd = creat(lock, FMASK)) < 0)
  110.             panic("Cannot open temp file %s!\n", lock);
  111.         savelev(nfd);
  112.         (void) close(nfd);
  113.         }
  114.     (void) lseek(fd, 0L, 0);
  115.     (void) getlev(fd);
  116.     (void) close(fd);
  117.     (void) unlink(SAVEF);
  118.     (void) delicon(SAVEF);
  119.     if(Punished) {
  120.         for(otmp = fobj; otmp; otmp = otmp->nobj)
  121.             if(otmp->olet == CHAIN_SYM) goto chainfnd;
  122.         panic("Cannot find the iron chain?");
  123.     chainfnd:
  124.         uchain = otmp;
  125.         if(!uball)
  126.             {
  127.             for(otmp = fobj; otmp; otmp = otmp->nobj)
  128.                 if(otmp->olet == BALL_SYM && otmp->spe)
  129.                     goto ballfnd;
  130.             panic("Cannot find the iron ball?");
  131.             ballfnd:
  132.             uball = otmp;
  133.             }
  134.     }
  135. #ifndef QUEST
  136.     setsee();  /* only to recompute seelx etc. - these weren't saved */
  137. #endif QUEST
  138.     docrt();
  139. }
  140.  
  141. struct obj *
  142. restobjchn(fd)
  143. register int fd;
  144. {
  145.     register struct obj *otmp, *otmp2;
  146.     register struct obj *first = 0;
  147.     int xl;
  148. #ifdef lint
  149.     /* suppress "used before set" warning from lint */
  150.     otmp2 = 0;
  151. #endif lint
  152.     while(1)
  153.         {
  154.         mread(fd, (char *) &xl, sizeof(xl));
  155.         if(xl == -1) break;
  156.         otmp = newobj(xl);
  157.         if(!first) first = otmp;
  158.         else otmp2->nobj = otmp;
  159.         mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
  160.         if(!otmp->o_id)   /* from MKLEV */
  161.             otmp->o_id = flags.ident++;
  162.         otmp2 = otmp;
  163.         }
  164.     if(first && otmp2->nobj)
  165.         {
  166.         pline("Restobjchn: error reading objchn.");
  167.         impossible();
  168.         otmp2->nobj = 0;
  169.         }
  170.     return(first);
  171. }
  172.  
  173. struct monst *
  174. restmonchn(fd)
  175. register int fd;
  176. {
  177.     register struct monst *mtmp, *mtmp2;
  178.     register struct monst *first = 0;
  179.     int xl;
  180.     
  181. #ifdef FUNNYRELOC
  182.     struct permonst *monbegin;
  183.  
  184.     mread(fd, (char *)&monbegin, sizeof(monbegin));
  185. #endif
  186.  
  187. #ifdef lint
  188.     /* suppress "used before set" warning from lint */
  189.     mtmp2 = 0;
  190. #endif lint
  191.     while(1)
  192.         {
  193.         mread(fd, (char *) &xl, sizeof(xl));
  194.         if(xl == -1) break;
  195.         mtmp = newmonst(xl);
  196.         if(!first) first = mtmp;
  197.         else mtmp2->nmon = mtmp;
  198.         mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
  199. #ifdef DEBUGMON
  200.         myprintf("Read Monster #%d", mtmp->data);
  201. #endif
  202.         mtmp->data = &mons[ (int) mtmp->data ];
  203.         if(!mtmp->m_id) {         /* from MKLEV */
  204.             mtmp->m_id = flags.ident++;
  205. #ifndef NOWORM
  206.             if(mtmp->data->mlet == 'w' && getwn(mtmp)){
  207.             initworm(mtmp);
  208.             mtmp->msleep = 0;
  209.             }
  210. #endif NOWORM
  211.         }
  212.         if(mtmp->minvent)
  213.             mtmp->minvent = restobjchn(fd);
  214.         mtmp2 = mtmp;
  215.         }
  216.     if(first && mtmp2->nmon){
  217.         pline("Restmonchn: error reading monchn.");
  218.         impossible();
  219.         mtmp2->nmon = 0;
  220.     }
  221.     return(first);
  222. }
  223.